/*
 * UserManager.cpp
 *
 *  Created on: Jan 24, 2015
 *      Author: wasp
 */

#include "UserManager.hpp"
#include <QMap>
#include <QDebug>

#include <bb/device/CellularNetworkInfo>
#include <bb/system/phone/Phone>

#include "../applicationui.hpp"

namespace de
{
    namespace heimweghelden
    {
        namespace user
        {
            const QString UserManager::PUBKEY = "PUBKEY";
            const QString UserManager::USERID = "USERID";

            UserManager* UserManager::_instance = 0;

            UserManager* UserManager::getInstance(QObject* parent) {
                if (_instance == 0) {
                    _instance = new UserManager(parent);
                }
                return _instance;
            }
            void UserManager::drop() {
                if (_instance != 0) {
                    delete _instance;
                    _instance = 0;
                }
            }
            UserManager::UserManager(QObject* parent) :
                                    QObject(parent)
            {
                if (parent != NULL) {
                    ApplicationUI* ui = reinterpret_cast<ApplicationUI*>(parent);
                    if (ui) {
                        app = ui;
                    }
                }
                _data = new QHash<QString, QString>;
                bb::device::CellularNetworkInfo *cellularNetworkInfo = new bb::device::CellularNetworkInfo(0, this);
                bb::system::phone::Phone* phone = new bb::system::phone::Phone(this);
                QString userid;
                userid.append(cellularNetworkInfo->mobileNetworkCode());
                userid.append("|");
                userid.append(cellularNetworkInfo->mobileCountryCode());
                userid.append("|");


                bool bbpinfound;
                bool phonefound;
                bb::system::phone::Line bbpin;
                bb::system::phone::Line phoneid;

                QMap<QString, bb::system::phone::Line> lines = phone->lines();
                foreach(bb::system::phone::Line line, lines) {
                    switch(line.type()) {
                        case bb::system::phone::LineType::Cellular:
                            phoneid = line;
                            phonefound = true;
                            break;
                        case bb::system::phone::LineType::VideoChat:
                            bbpin = line;
                            bbpinfound = true;
                            break;
                        default:;
                    }
                }
                if (phonefound) {
                    userid.append(phoneid.address());
                } else if(bbpinfound) {
                    userid.append(bbpin.address());
                }
                else {
                    userid = QString::null;
                }
                if (userid != QString::null) {
                    _data->insert(USERID, userid);
                }
            }

            UserManager::~UserManager() {
                delete _data;
            }

            void UserManager::set(QString k, QString value) {
                if (k == USERID) {
                    return;
                }
                _data->insert(k, value);
            }
            QString UserManager::get(QString k) {
                return _data->value(k);
            }



        } /* namespace user */
    } /* namespace heimweghelden */
} /* namespace de */
